package com.kintreda.ieco_server.module.autoreply.web;

import com.kintreda.ieco_server.bean.system.KeyWord;
import com.kintreda.ieco_server.core.web.R;
import com.kintreda.ieco_server.module.autoreply.service.IKeyWordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Created by IntelliJ IDEA
 * @Project_Name ieco_server
 * @Package com.kintreda.ieco_server.module.autoreply.web
 * @Author ZhongYao_H
 * @Date 2019/5/21:19:17
 * @Year 2019 Month 05 Day 21
 * @Date_zh 2019 年 五月 星期二
 */
@Controller
@RequestMapping("/autorely")
public class AutoreplyController {

    @Autowired
    IKeyWordService wordService;


    @RequestMapping("/list")
    public ModelAndView citylist(){
        return new ModelAndView("autoreply/autoreply-list");
    }

    @RequestMapping(value = "/query",method = RequestMethod.POST)
    @ResponseBody
    public Object query(String keyword){

        if (StringUtils.isNotBlank(keyword)){
            return wordService.query("from KeyWord where keyword like ? ",new Object[]{"%"+keyword+"%"});
        }else {
            return wordService.query("from KeyWord ",null);
        }

    }

    @RequestMapping("/add")
    public ModelAndView add(){
        return new ModelAndView("autoreply/autoreply-add");
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Object citylist(KeyWord keyWord){
        if (wordService.isadd(keyWord.getKeyword())){
            return R.error("此关键字已有自动回复!");
        }
        if (keyWord.getPrice()==null || keyWord.getUnit()==null || keyWord.getPrice()=="" || keyWord.getUnit()==""){
            keyWord.setPrice("N.NN");
            keyWord.setUnit("N.NN");
        }
        keyWord.setKeyword(keyWord.getKeyword().toUpperCase());
        wordService.save(keyWord);
        return R.ok("添加成功!");
    }

    @RequestMapping(value = "/del",method = RequestMethod.POST)
    @ResponseBody
    public Object del(String id){
        wordService.deleteByPK(id);
        return R.ok("操作成功!");
    }

    @RequestMapping("/edit")
    public ModelAndView edit(String id){
        ModelAndView andView = new ModelAndView("autoreply/autoreply-edit");
        andView.addObject("reply",wordService.get(id));
        return andView;
    }

    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ResponseBody
    public Object edit(KeyWord keyWord){
        keyWord.setKeyword(keyWord.getKeyword().toUpperCase());
        KeyWord olaKeyword = wordService.get(keyWord.getId());
        if (olaKeyword==null){
            return R.error("修改回复词条不存在!");
        }

        olaKeyword.setPrice(keyWord.getPrice());
        olaKeyword.setKeyword(keyWord.getKeyword());
        olaKeyword.setBody(keyWord.getBody());
        olaKeyword.setTypeA(keyWord.getTypeA());
        olaKeyword.setTypeB(keyWord.getTypeB());
        olaKeyword.setTypeC(keyWord.getTypeC());
        wordService.update(olaKeyword);
        return R.ok("修改成功!");
    }




}
